package com.test.daily.leetcode.y2021.m12.day1206;

/**
 * truncateSentence
 *
 * @author Tom on 2021/12/6
 */
public class Code01_TruncateSentence {
    public static void main(String[] args) {
        Code01_TruncateSentence ct = new Code01_TruncateSentence();
        String s = "Hello how are you Contestant";
        String ans = ct.truncateSentence(s,4);
        System.out.println(ans);
    }
    public String truncateSentence(String s, int k){
        StringBuilder builder = new StringBuilder();
        int i = 0;
        int j = 0;
        while(i < s.length()){
            builder.append(s.charAt(i));
            if(s.charAt(i) == ' '){
                j++;
                if(j == k){
                    break;
                }
            }
            i++;
        }
        return builder.toString().trim();
    }
}
